package com.t3go.t3_map.base;

import android.location.Location;

import com.shareware.common.utils.LogExtKt;
import com.t3go.passenger.base.utils.RxUtil;
import com.t3go.t3_map.service.LocationRouterServiceImpl;
import com.t3map.api.T3MapsInitializer;
import com.t3map.api.location.entity.T3MapLocation;
import com.t3map.api.map.monitor.MapMonitor;
import com.t3map.core.map.T3LocationSource;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class MapSourceHelper {

    public static Disposable activeLocationSource(long interval, final T3LocationSource.OnLocationChangedListener onLocationChangedListener) {
        return Observable.interval(interval, TimeUnit.MILLISECONDS)
                .compose(RxUtil.applySchedulers())
                .subscribe(aLong -> callReport(onLocationChangedListener, aLong));
    }

    public static void deactivateLocationSource(Disposable disposable) {
        if (disposable != null) {
            disposable.dispose();
        }
    }

    private static void callReport(T3LocationSource.OnLocationChangedListener onLocationChangedListener, Long along) {
        //ILocationRouterService mRouterService = ARouter.getInstance().navigation(ILocationRouterService.class);
        LocationRouterServiceImpl mRouterService = new LocationRouterServiceImpl();
        T3MapLocation result = mRouterService.getCurLocation();
        if (onLocationChangedListener != null) {
            Location location;
            if (result == null) {
                location = new Location("");
            } else {
                location = new Location(result.getLocationType() + "");
                //设置经纬度以及精度
                location.setLatitude(result.getLat());
                location.setLongitude(result.getLng());
                location.setAccuracy(result.getAccuracy());
                try {
                    location.setBearing(result.getBearing());
                } catch (Exception e) {
                    e.printStackTrace();
                    MapMonitor.trackTMapLocationBearing(T3MapsInitializer.getMapType(), String.valueOf(result.getBearing()), result.toStr());
                    LogExtKt.log("TLocationError", "t3mapLocation bearing:" + result.getBearing());
                }
            }
            onLocationChangedListener.onLocationChanged(location);
        }


    }
}
